package cntv.player.core.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 共享参数设置工具类
 * @author zengliang
 *
 */
public class SprefUtils {

	private static SprefUtils spUtils;
	private static SharedPreferences sp;

	private SprefUtils() {

	}

	public static SprefUtils getInstance(Context context) {
		if (sp == null && context != null) {
			sp = context.getSharedPreferences(context.getPackageName(),Context.MODE_PRIVATE);
		}
		if (spUtils == null) {
			spUtils = new SprefUtils();
		}

		return spUtils;
	}

	public String getString(String key) {
		if(sp == null){
			return null;
		}
		return sp.getString(key, "");
	}

	public void putString(String key, String value) {
		if(sp == null){
			return;
		}
		Editor edit = sp.edit();
		edit.putString(key, value);
		edit.commit();
	}

	public int getInt(String key) {
		if(sp == null){
			return 0;
		}
		return sp.getInt(key, 0);
	}
	
	public int getInt(String key, int defalutVal) {
		if(sp == null){
			return defalutVal;
		}
		return sp.getInt(key, defalutVal);
	}

	public void putInt(String key, int value) {
		if(sp == null){
			return;
		}
		Editor edit = sp.edit();
		edit.putInt(key, value);
		edit.commit();
	}

	public long getLong(String key) {
		if(sp == null){
			return 0;
		}
		return sp.getLong(key, 0);
	}

	public void putLong(String key, long value) {
		if(sp == null){
			return;
		}
		Editor edit = sp.edit();
		edit.putLong(key, value);
		edit.commit();
	}

	public Boolean getBoolean(String key) {
		if(sp == null){
			return false;
		}
		return sp.getBoolean(key, false);
	}

	public void putBoolean(String key, boolean value) {
		if(sp == null){
			return;
		}
		Editor edit = sp.edit();
		edit.putBoolean(key, value);
		edit.commit();
	}
	
	public void remove(String key){
		if(sp == null){
			return;
		}
		Editor edit = sp.edit();
		edit.remove(key);
		edit.commit();
	}

}
